﻿using System.IO;
using System.Linq;
using System.Text;
using RikaScript.Libs.Base;

namespace RikaScript.Libs
{
    /*
     * 更新日志
     * 0.1：创建类库，实现读取、追加、覆盖写入
     */
    [Library(Name = "file", Help = "RikaScript 标准文件读写类库", Version = "v0.1.0")]
    public class FileLib : ScriptLibBase
    {
        private Encoding _encoding = Encoding.UTF8;

        [Method(Help = "设置以后操作文件所用的编码")]
        public void setEncoding(object encoding)
        {
            _encoding = Encoding.GetEncoding(encoding.String());
        }

        [Method(Help = "获取当前操作文件所用的编码")]
        public object getEncoding()
        {
            return _encoding.ToString();
        }

        [Method(Help = "读取文件全部内容")]
        public object read(object path)
        {
            return File.ReadAllText(path.String(),_encoding);
        }

        [Method(Help = "读取文件全部内容，返回List，一个元素是一行文本")]
        public object readLines(object path)
        {
            return File.ReadAllLines(path.String(),_encoding).ToList();
        }

        [Method(Help = "覆盖写入文件")]
        public void write(object path, object text)
        {
            File.WriteAllText(path.String(), text.String(),_encoding);
        }

        [Method(Help = "追加写入文件")]
        public void append(object path, object text)
        {
            File.AppendAllText(path.String(), text.String(),_encoding);
        }
    }
}